package com.daqian.mall.rest;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class RestResult {

    private RestResult() {

    }

    public static final int SUCCESS = 0;

    public static final String SUCCESS_STR = "success";

    private int code;

    private String msg;


    private Map<String, Object> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getData() {
        return this.data;
    }

    public <T> T getData(String key, Class<T> clazz) {
        return JSONObject.parseObject(JSONObject.toJSONString(getData().get(key)), clazz);
    }


    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    /**
     * 构建返回json
     *
     * @param msgCode 响应代码
     * @param msgInfo 响应说明
     * @return json对象
     */
    public static RestResult msg(int msgCode, String msgInfo) {
        RestResult restResult = new RestResult();
        restResult.setCode(msgCode);
        restResult.setMsg(msgInfo);
        return restResult;
    }

    /**
     * 构建返回json
     *
     * @param msgCode 响应代码
     * @param msgInfo 响应说明
     * @param dataMap 数据（Map集合）
     * @return
     */
    private static RestResult msg(int msgCode, String msgInfo, Map<String, Object> dataMap) {
        RestResult restResult = msg(msgCode, msgInfo);
        restResult.setData(dataMap);
        return restResult;
    }


    /**
     * 构建返回json
     *
     * @param map
     * @return
     */
    public static RestResult success(Map map) {
        return msg(MessageConst.Msg.SUCCESS, map);
    }

    /**
     * 构建返回json
     *
     * @param obj
     * @return
     */
    public static RestResult success(Object obj) {
        Map<String, Object> map = new HashMap<>();
        String className = obj.getClass().getSimpleName();
        className = Character.toLowerCase(className.charAt(0)) + className.substring(1);
        map.put(className, obj);
        return success(map);
    }


    /**
     * 构建返回json
     *
     * @return
     */
    public static RestResult success() {
        return msg(MessageConst.Msg.SUCCESS);
    }


    /**
     * 返回失败 不说明具体原因
     *
     * @return
     */
    public static RestResult fail() {
        return msg(MessageConst.Msg.HANDLE_FAIL);
    }

    /**
     * 返回失败，自定义原因
     *
     * @param reason
     * @return
     */
    public static RestResult fail(String reason) {
        return msg(-1, reason);
    }

    /**
     * 返回失败，自定义原因
     *
     * @param code
     * @param reason
     * @return
     */
    public static RestResult fail(int code, String reason) {
        return msg(code, reason);
    }


    /**
     * 构建返回json
     *
     * @param msg
     * @return
     */
    public static RestResult msg(MessageConst.Msg msg) {
        return msg(msg.id, msg.info);
    }

    /**
     * 构建返回json
     *
     * @param msg
     * @return
     */
    public static RestResult msg(MessageConst.Msg msg, Map dataMap) {
        return msg(msg.id, msg.info, dataMap);
    }


    /**
     * 构建返回json（不一定是出错了 返回状态码为当前时间戳 原因自定）
     *
     * @param info
     * @return
     */
    public static RestResult msg(String info) {
        return msg((int) System.currentTimeMillis(), info);
    }


    public static int getTimeDifft(Date minDate, Date maxDate) {
        long diff = maxDate.getTime() - minDate.getTime();
        return (int) (diff / 1000);
    }

    /**
     * 获取今天剩余秒
     *
     * @return
     */
    public static long getTodayResidualTime() {

        Calendar min = Calendar.getInstance();

        Calendar max = Calendar.getInstance();
        max.set(Calendar.HOUR_OF_DAY, 23);
        max.set(Calendar.MINUTE, 59);
        max.set(Calendar.SECOND, 59);

        min.get(Calendar.HOUR_OF_DAY);
        min.get(Calendar.MINUTE);
        min.get(Calendar.SECOND);

        return max.getTimeInMillis() - min.getTimeInMillis() + 1000;
    }
}
